package insta.project.utils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;

import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.os.Environment;

public class FileHelper {
	public static void saveImageToSdCard(String strDestination, Bitmap bmpFile) {
		try {
			String strFileName = new File(strDestination).getName();
			CompressFormat compressFormat = strDestination.contains(".png") ? Bitmap.CompressFormat.PNG
					: Bitmap.CompressFormat.JPEG;
			File folder = new File(Environment.getExternalStorageDirectory()
					+ "/track_pictures");
			if (!folder.exists()) {
				folder.mkdir();
			}
			// FIXME : add behaviour when failed creating folder (user gived
			// enough permissions ?)

			// Output image file
			String path = Environment.getExternalStorageDirectory().toString()
					+ "/track_pictures";
			OutputStream fOut = null;
			File file = new File(path, strFileName);
			fOut = new FileOutputStream(file);
			bmpFile.compress(compressFormat, 85, fOut);
			fOut.flush();
			fOut.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}